package com.grape.action.common;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.cxf.jaxrs.model.ParameterType;
import org.apache.poi.ss.formula.functions.T;
import org.apache.struts2.ServletActionContext;
import org.springframework.data.domain.Page;

import com.grape.domain.base.Area;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T>{
	
	protected T model;

	@Override
	public T getModel() {
		return model;
	}
	
	protected Integer page;
	protected Integer rows;
	public void setPage(Integer page) {
		this.page = page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}
	

	// 构造器 完成model实例化
	public BaseAction() {
		// 1.获取继承我的小伙伴给我的泛型
		// 比如AreaAction extends BaseAction<Area>中的Area
		
		// 编译后反省参数擦除依然会残留下来,可以这样或获取到
		Type genericSuperclass = this.getClass().getGenericSuperclass();
		ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
		Class<T> modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
		try {
			model =  modelClass.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("模型构造失败");
		} 
	}
	
	
	protected void pushPageDataToValueStack(Page<T> page) {
		Map<String,Object> mp = new HashMap<>();
		mp.put("total", page.getTotalElements());
		mp.put("rows", page.getContent());
	
		ServletActionContext.getContext().getValueStack().push(mp);
	}
}
